package com.flyme.module.base.provider.controller;


import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.common.core.utils.TreeUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysHtmlRemoteService;
import com.flyme.module.base.client.entity.SysHtml;
import com.flyme.module.base.provider.service.SysHtmlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "协议类型管理")
@RestController
public class SysHtmlController extends BaseController<SysHtmlService, SysHtml> implements SysHtmlRemoteService {

    @ApiOperation(value = "协议分类")
    @Override
    public ResultBody tree(@ModelAttribute(value = "html") SysHtml html) {
        CriteriaQuery qw = new CriteriaQuery<EntityMap>();
        qw.select("id", "id value", "htmlTitle title", "parentId", "isParent", "sortOrder");
        qw.orderByAsc("sortOrder");
        List list = bizService.listMaps(qw);
        List<Map<String, Object>> map = TreeUtils.buildTree(list, CommonConstants.PARENT_ID);
        return ResultBody.success(map);
    }

    @Override
    public ResultBody get(@PathVariable Long id) {
        SysHtml html = bizService.getById(id);
        return ResultBody.success(html);
    }

    @ApiOperation(value = "编辑协议", notes = "编辑协议")
    @Override
    public ResultBody edit(SysHtml html) {
        if(ObjectUtils.isEmpty(html.getId())){
            html.setHtmlStatus(CommonConstants.ENABLED);
            html.setIsParent(CommonConstants.DISABLED);
        }
        boolean result = bizService.saveOrUpdate(html);
        return ResultBody.success("操作成功",result);
    }

    @ApiOperation(value = "删除协议", notes = "删除协议")
    @Override
    public ResultBody delById(@RequestParam Long id) {
        Boolean tag = bizService.removeById(id);
        return ResultBody.success(tag);
    }


}
